// 检测是否在小程序环境中并添加图片预览功能
const isInMiniProgram = window.wx && window.wx.miniProgram;
if (isInMiniProgram) {
    document.addEventListener('DOMContentLoaded', function() {
        // 检测是否在小程序环境中
        
        // 获取所有查看文件按钮
        const fileLinks = document.querySelectorAll('a[href^="https://oss."], a[href^="https://oss2."]');
        
        fileLinks.forEach(link => {
            link.addEventListener('click', function(e) {
                const imgUrl = this.getAttribute('href');
                const imgExt = imgUrl.split('.').pop().toLowerCase();
                
                // 判断是否为图片文件
                if(['jpg', 'jpeg', 'png', 'gif', 'webp'].includes(imgExt)) {
                    // 如果在小程序环境中，阻止默认行为并显示弹窗
                    if(isInMiniProgram) {
                        e.preventDefault();
                        showImageModal(imgUrl);
                    }
                }
            });
        });
    });

}

// 显示图片弹窗
function showImageModal(imgUrl) {
    // 创建模态框元素
    let modal = document.createElement('div');
    modal.className = 'modal fade';
    modal.id = 'imagePreviewModal';
    modal.setAttribute('tabindex', '-1');
    
    modal.innerHTML = `
        <div class="modal-dialog modal-dialog-centered modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">图片预览</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body text-center">
                    <img src="${imgUrl}" class="img-fluid" style="max-height: 70vh;">
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
                </div>
            </div>
        </div>
    `;
    
    // 添加到页面
    document.body.appendChild(modal);
    
    // 显示模态框
    const modalInstance = new bootstrap.Modal(modal);
    modalInstance.show();
    
    // 模态框关闭后移除DOM元素
    modal.addEventListener('hidden.bs.modal', function() {
        document.body.removeChild(modal);
    });
}
